package com.ganzi.ganziitem.netty.util;

import java.math.BigInteger;

/**
* 通信格式转换
*
* Java和一些windows编程语言如c、c++、delphi所写的网络程序进行通讯时，需要进行相应的转换
* 高、低字节之间的转换
* windows的字节序为低字节开头
* linux,unix的字节序为高字节开头
* java则无论平台变化，都是高字节开头
*/

public class FormatTransfer {
/**
  * 将int转为低字节在前，高字节在后的byte数组
  * @param n int
  * @return byte[]
  */
public static byte[] toLH(int n) {
  byte[] b = new byte[4];
  b[0] = (byte) (n & 0xff);
  b[1] = (byte) (n >> 8 & 0xff);
  b[2] = (byte) (n >> 16 & 0xff);
  b[3] = (byte) (n >> 24 & 0xff);
  return b;
}

/**
  * 将int转为高字节在前，低字节在后的byte数组
  * @param n int
  * @return byte[]
  */
public static byte[] toHH(int n) {
  byte[] b = new byte[4];
  b[3] = (byte) (n & 0xff);
  b[2] = (byte) (n >> 8 & 0xff);
  b[1] = (byte) (n >> 16 & 0xff);
  b[0] = (byte) (n >> 24 & 0xff);
  return b;
}

/**
  * 将short转为低字节在前，高字节在后的byte数组
  * @param n short
  * @return byte[]
  */
public static byte[] toLH2(short n) {
  byte[] b = new byte[2];
  b[0] = (byte) (n & 0xff);
  b[1] = (byte) (n >> 8 & 0xff);
  return b;
}

/**
  * 将short转为高字节在前，低字节在后的byte数组
  * @param n short
  * @return byte[]
  */
public static byte[] toHH2(short n) {
  byte[] b = new byte[2];
  b[1] = (byte) (n & 0xff);
  b[0] = (byte) (n >> 8 & 0xff);
  return b;
}

 
 

/**
  * 将低字节数组转换为int
  * @param b byte[]
  * @return int
  */
public static int lBytesToInt(byte[] b) {
  int s = 0;
  for (int i = 0; i < 3; i++) {
    if (b[3-i] >= 0) {
    s = s + b[3-i];
    } else {
    s = s + 256 + b[3-i];
    }
    s = s * 256;
  }
  if (b[0] >= 0) {
    s = s + b[0];
  } else {
    s = s + 256 + b[0];
  }
  return s;
}


/**
  * 高字节数组到short的转换
  * @param b byte[]
  * @return short
  */
public static short hBytesToShort(byte[] b) {
  int s = 0;
  if (b[0] >= 0) {
    s = s + b[0];
    } else {
    s = s + 256 + b[0];
    }
    s = s * 256;
  if (b[1] >= 0) {
    s = s + b[1];
  } else {
    s = s + 256 + b[1];
  }
  short result = (short)s;
  return result;
}

/**
  * 低字节数组到short的转换
  * @param b byte[]
  * @return short
  */
public static short lBytesToShort(byte[] b) {
  int s = 0;
  if (b[1] >= 0) {
    s = s + b[1];
    } else {
    s = s + 256 + b[1];
    }
    s = s * 256;
  if (b[0] >= 0) {
    s = s + b[0];
  } else {
    s = s + 256 + b[0];
  }
  short result = (short)s;
  return result;
}
 

/**
  * 打印byte数组
  */
public static void printBytes(byte[] bb) {
  int length = bb.length;
  for (int i=0; i<length; i++) {
    System.out.print(bb[i] + " ");
  }
  System.out.println("");
}
public static void main(String[] args) {
  String gao = "00EF";
  String di = "FFFF";
  String nn = gao.trim()+di.trim();
  BigInteger bi = new BigInteger(nn, 16);
  System.out.println("---:"+bi.doubleValue());
}

public String intToHexString(int a, int len) {
	len <<= 1;
	String hexString = Integer.toHexString(a);
	int b = len - hexString.length();
	if (b > 0) {
		for (int i = 0; i < b; i++) {
			hexString = "0" + hexString;
		}
	}
	return hexString;
}

  public static byte[] copy(byte[] data, int srcpos, int length) {
    byte[] dest = new byte[length];
    System.arraycopy(data, srcpos, dest, 0, length);
    return dest;
  }

  //4字节16进制转换成int  无符号
  public static long byteToIntForHex(byte[] bb,byte[] aa){
    String gao = CRC16M.getBufHexStr(bb);
    String di = CRC16M.getBufHexStr(aa);
    String nn = gao.trim()+di.trim();
    BigInteger bi = new BigInteger(nn, 16);
    return bi.longValue();
  }

  //2字节16进制转换成int  无符号
  public static int byteToIntForHex(byte[] bb){
    String gao = CRC16M.getBufHexStr(bb).trim();
    BigInteger bi = new BigInteger(gao, 16);
    return bi.intValue();
  }
}
